package cc.chinagps.gateway.client;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class TaskManager {
    ScheduledExecutorService timer = Executors.newScheduledThreadPool(4);

    private TaskManager() {
    }

    private static class SingletonHolder {
        private static final TaskManager INSTANCE = new TaskManager();
    }

    public static final TaskManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void exec(Runnable task, int period) {
        timer.scheduleAtFixedRate(task, 1000, period, TimeUnit.MILLISECONDS);
    }

    public void exec(Runnable task, int delay, int period) {
        timer.scheduleAtFixedRate(task, delay, period, TimeUnit.MILLISECONDS);
    }

    public void exec(Runnable task) {
        timer.execute(task);
    }

    public void execWithDelay(Runnable task, int period) {
        timer.schedule(task, period, TimeUnit.MILLISECONDS);
    }


}
